﻿@{
    ViewBag.Title = "组织架构";
    Layout = "~/Views/Shared/_ComplexLayout.cshtml";
}
<script type="text/javascript" src="@Url.Content("~/Scripts/common.js")"></script>
<script type="text/javascript">
    var lastIndex;
    var postLastIndex;
    var organizeId;
    $(function () {
        //组织架构
        $('#tt').treegrid({
            title: '组织架构',
            iconCls: 'icon-edit',
            nowrap: false,
            rownumbers: true,
            animate: false,
            collapsible: false,
            singleSelect: true,
            fit: true,
            url: '@Url.Action("OrganizeJsonList")',
            idField: 'Id',
            treeField: 'Id',
            toolbar: [{
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    reload();
                }
            }, '-',
            {
                text: '添加',
                iconCls: 'icon-add',
                handler: function () {
                    append();
                }
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    remove();
                }
            }, '-', {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    var row = $('#tt').treegrid('getSelected');
			        if (row){
				        $('#tt').treegrid('endEdit',row.Id);
			        }
                }
            }, '-', {
                text: '撤销',
                iconCls: 'icon-undo',
                handler: function () {
                    var row = $('#tt').treegrid('getSelected');
			        if (row){
				        $('#tt').treegrid('cancelEdit',row.Id);
			        }
                }
            }],
            frozenColumns: [[
                    {title:'',field:'Id',width:76,
                        formatter: function (value){
                            return '';
                        }
                     }
	                ,{ title: '编码', field: 'Code', width: 70, editor:'text'
	                }
				]],
            columns: [[
					{ field: 'OrganizeName', title: '部门名称', width: 300, editor:'text' }
				]],
            onBeforeLoad: function (row, param) {
                if (row) {
//                    $(this).treegrid('options').url = 'treegrid_subdata.json';
                } else {
                    $(this).treegrid('options').url = '@Url.Action("OrganizeJsonList")';
                }
            },
            onContextMenu: function (e, row) {
                e.preventDefault();
                $(this).treegrid('unselectAll');
                $(this).treegrid('select', row.Id);
                $('#mm').menu('show', {
                    left: e.pageX,
                    top: e.pageY
                });
            }
            ,onClickRow:function(row){//运用单击事件实现一行的编辑结束，在该事件触发前会先执行onAfterEdit事件
             var rowIndex = row.Id;
             organizeId = rowIndex;
             if (lastIndex != rowIndex){
                $('#tt').treegrid('endEdit', lastIndex);
             }
             //刷新岗位列表
             $('#post').datagrid('reload', {'organizeId':rowIndex})
            }
            ,onDblClickRow:function(row){//运用双击事件实现对一行的编辑
             var rowIndex = row.Id;
             if (lastIndex != rowIndex){
              $('#tt').treegrid('endEdit', lastIndex);
              $('#tt').treegrid('beginEdit', rowIndex);
              lastIndex = rowIndex;
             }
            }
            ,onBeforeEdit:function(row){
                //这里是功能实现的主要步骤和代码
                var codeCoclum = $('#tt').treegrid('getColumnOption','Code');
                var organizeNameCoclum = $('#tt').treegrid('getColumnOption','OrganizeName');
                codeCoclum.editor='text';
                organizeNameCoclum.editor='text';
            }
            ,onAfterEdit:function(row,changes){
                     var rowdata = { "Id":row.Id
                             , "Code":row.Code
                             , "OrganizeName":row.OrganizeName
                             , "ParentId":row._parentId }
                 $.ajax({
                      url:"@Url.Action("InsertOrEditOrganize")" ,
                      data: rowdata,
                      success: function(text){
                              $.messager.show({
				                    title:'组织架构',
				                    msg:text,
				                    timeout:1500,
				                    showType:'slide'
			                    });
                      }
                    });
            }
            ,onLoadSuccess:function(row,data){
                if(data.rows != undefined)
                {
                    organizeId = data.rows[0].Id;
                    $('#tt').treegrid('select', data.rows[0].Id);
                    //刷新岗位列表
                    $('#post').datagrid('reload', {'organizeId':data.rows[0].Id})
                }
            }

        });

        //岗位权限
			$('#post').datagrid({
				toolbar:[{
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                     $('#post').datagrid('reload');
                }
              },'-',{
					text:'添加',
					iconCls:'icon-add',
					handler:function(){
                        $("#nomsg").hide();
					    $('#post').datagrid('endEdit', postLastIndex);
						$('#post').datagrid('appendRow',{
							Id:guidGenerator(),
							Code:'',
							PostName:''
						});
						var postLastIndex = $('#post').datagrid('getRows').length-1;
						$('#post').datagrid('selectRow', postLastIndex);
						$('#post').datagrid('beginEdit', postLastIndex);
					}
				},'-',{
					text:'删除',
					iconCls:'icon-remove',
					handler:function(){
						var row = $('#post').datagrid('getSelected');
						if (row){
						   var index = $('#post').datagrid('getRowIndex', row);
                           $.messager.confirm('岗位管理', '您确定要删除改记录吗?', function (r) {
                                            if (r) {
                                                var node = $('#tt').treegrid('getSelected');
                                                if (node) {
                                                    $.ajax({
                                                      url:"/Post/DeletePostById?Id="+row.Id ,
                                                      data: node,
                                                      success: function(text){
                                                         $('#post').datagrid('deleteRow', index);
                                                         $.messager.show({
				                                            title:'岗位管理',
				                                            msg:text,
				                                            timeout:1500,
				                                            showType:'slide'
			                                            });
                                                      }
                                                    });
                                                }
                                            }
                             });
						}
					}
				},'-',{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						$('#post').datagrid('acceptChanges');
					}
				},'-',{
					text:'撤销',
					iconCls:'icon-undo',
					handler:function(){
						$('#post').datagrid('rejectChanges');
					}
				}],
                onClickRow:function(rowIndex){
                     if (postLastIndex != rowIndex){
                      $('#post').datagrid('endEdit', postLastIndex);
                     }
                },
				onDblClickRow:function(rowIndex){
					if (postLastIndex != rowIndex){
						$('#post').datagrid('endEdit', postLastIndex);
						$('#post').datagrid('beginEdit', rowIndex);
					}
					postLastIndex = rowIndex;
				},
                onLoadSuccess:function(data){
                    if(data.total == 0)
                    {
                         $("#nomsg").show();
                    }
                    else
                    {
                        $("#nomsg").hide();
                    }
                }
                ,onAfterEdit:function(rowIndex, row, changes){
                     var rowdata = { "Id":row.Id
                             , "Code":row.Code
                             , "OrganizeId":organizeId
                             , "PostName":row.PostName};
                    $.ajax({
                        url:"@Url.Action("InsertOrEditPost", "Post")" ,
                        data: rowdata,
                        success: function(text){
                                    $.messager.show({
				                        title:'岗位管理',
				                        msg:text,
				                        timeout:1500,
				                        showType:'slide'
			                        });
                                }
                        });
                }
			});
    });

    function reload() {
        var node = $('#tt').treegrid('getSelected');
        if (node) {
            $('#tt').treegrid('reload', node.code);
            $('#post').datagrid('reload',node.Id);
        } else {
            $('#tt').treegrid('reload');
            $('#post').datagrid('reload');
        }
    }

    function append() {
        var data = [{
            Id : guidGenerator(),
            Code: '',
            OrganizeName: '',
        }];
        var node = $('#tt').treegrid('getSelected');
        $('#tt').treegrid('append', {
            parent: (node ? node.Id : null),
            data: data
        });
        $('#tt').treegrid('unselect', node.Id);
        $('#tt').treegrid('select', data[0].Id);
        $('#tt').treegrid('beginEdit', data[0].Id);
        lastIndex = data[0].Id;
    }

    function remove() {
        $.messager.confirm('组织架构', '您确定要删除改记录吗?', function (r) {
                            if (r) {
                                var node = $('#tt').treegrid('getSelected');
                                if (node) {
                                    $.ajax({
                                      url:"DeleteOrganizeById?Id="+node.Id ,
                                      data: node,
                                      success: function(text){
                                         $('#tt').treegrid('remove', node.Id);
                                         $.messager.show({
				                            title:'组织架构',
				                            msg:text,
				                            timeout:1500,
				                            showType:'slide'
			                            });
                                      }
                                    });
                                }
                            }
                        });
    }

    function permisionFormatter(value){
			return  "<a href='javascript:void(0);' onclick=\"openPermision('/Permision/Index?PostId="+ value +"&Type=Post','permision',600,600);\">设置</a>";
	}

    function openPermision(url)
    {
        $('#openXXXIframe')[0].src = 'abount:blank';
        $('#openXXXIframe')[0].src=url; 
        $('#openPermisionDiv').dialog('open'); 
    }

</script>
<div region="west" split="true" style="width: 500px; padding: 5px;" border="false"
    id="west">
    <table id="tt">
    </table>
    <div id="mm" class="easyui-menu" style="width: 120px;">
        <div onclick="append()">
            添加</div>
        <div onclick="remove()">
            删除</div>
    </div>
</div>
<div region="center" style="padding: 5px;" border="false">
    <table id="post" title="岗位管理" iconcls="icon-edit" singleselect="true"
        fit="true" rownumbers="true" idfield="Id" url="@Url.Action("PostJsonList", "Post")">
        <thead>
            <tr>
                <th field="Code" width="80" editor="text">
                    编码
                </th>
                <th field="PostName" width="260" editor="text">
                    岗位名称
                </th>
                <th field="Id" width="60" formatter="permisionFormatter">
                    权限
                </th>
            </tr>
        </thead>
    </table>
    <div id="nomsg" style="display: none; position: absolute; top: 85px; left: 5px; border-bottom: 1px #EEE dotted;
        border-right: 1px #EEE dotted; color: #999; width: 269px; padding-left: 160px;
        padding-bottom: 5px; padding-top: 5px">
        没有查询到相关数据!
    </div>
</div>

<div id="openPermisionDiv" class="easyui-window" closed="true" modal="true" title="权限树" style="width:600px;height:600px;">
     <iframe scrolling="auto" id='openXXXIframe' frameborder="0"  src="" style="width:100%;height:100%;"></iframe>
 </div> 

